package com.hzvdvmc.autosale.sale.ui;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.constant.PayModeEnum;
import com.hzvdvmc.autosale.constant.SpKey;
import com.hzvdvmc.autosale.constant.TimeConstant;
import com.hzvdvmc.autosale.sale.MainActivity;
import com.hzvdvmc.autosale.sale.bean.ShopListRespnse;
import com.hzvdvmc.autosale.util.LogUtil;
import com.hzvdvmc.autosale.util.SPUtils;
import com.uuzuche.lib_zxing.DisplayUtil;
import com.uuzuche.lib_zxing.activity.CodeUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by william on 2017/12/21.
 */

public class PayDialog extends BaseDialog {
    @BindView(R.id.iv_good)
    ImageView ivGood;
    @BindView(R.id.rbtn_wechatpay)
    RadioButton rbtnWechatpay;
    @BindView(R.id.rbtn_alipay)
    RadioButton rbtnAlipay;
    @BindView(R.id.rGp_payway)
    RadioGroup rGpPayway;
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;
    @BindView(R.id.tv_dialog_price)
    TextView tvDialogPrice;
    @BindView(R.id.tv_state)
    TextView tvState;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    @BindView(R.id.tv_name)
    TextView tvName;
    private ShopListRespnse.RetObjBean dataBean;

    public void setData(ShopListRespnse.RetObjBean dataBean) {
        this.dataBean = dataBean;

        loadUI(dataBean);

    }

    private void loadUI(ShopListRespnse.RetObjBean dataBean) {
        String url = dataBean.getPicUrl();
        Glide.with(getContext()).load(url).into(ivGood);
        tvDialogPrice.setText("￥:" + dataBean.getAPrice());
        tvName.setText(dataBean.getWaresName());
    }

    private CountDownTimer countDownTimer;

    public PayDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.dataBean = dataBean;
    }

    public PayDialog(@NonNull Context context) {
        this(context, R.style.MyDialog);
    }

    public String getWaresId() {
        if (dataBean != null) {
            return dataBean.getWaresId();
        }
        return null;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pay);
        ButterKnife.bind(this);
        setCanceledOnTouchOutside(true);
        //这里图片地址不是全部的
        addLisenter();
    }

    private void addLisenter() {
        rGpPayway.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rbtn_alipay:
                        getPayInfo(PayModeEnum.ALIPAY);
                        break;
                    case R.id.rbtn_wechatpay:
                        getPayInfo(PayModeEnum.WECHATPAY);
                        break;
                }
            }
        });

        ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (countDownTimer != null) {
                    countDownTimer.cancel();
                }
            }
        });

    }

    @Override
    public void show() {
        super.show();

        String st = SPUtils.getInstance().getString(SpKey.TIME_PAY);
        int sbt = Integer.parseInt(st);

        countDownTimer = new CountDownTimer(TimeConstant.second * sbt, TimeConstant.second) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvTime.setText(millisUntilFinished / 1000 + " s");
            }

            @Override
            public void onFinish() {
                if (isShowing()) {
                    dismiss();
                }
            }
        };

        countDownTimer.start();

        tvState.setText("");
    }

    private void getPayInfo(PayModeEnum wechatpay) {
        Activity ownerActivity = getOwnerActivity();
        if (ownerActivity != null) {
            MainActivity activity = (MainActivity) ownerActivity;
            activity.loadCodeValue(dataBean.getWaresId());
        }
    }


    public void setCode(String text) {
        LogUtil.i("qrcode value:" + text);
        Bitmap image = CodeUtils.createImage(text, DisplayUtil.dip2px(getContext(), 240f),
                DisplayUtil.dip2px(getContext(), 240f), null);
        ivQrcode.setImageBitmap(image);
    }

    public void updateState(String state) {
        tvState.setText(state);
    }



}
